<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="球队id" prop="teamId">
      <el-input v-model="dataForm.teamId" placeholder="球队id"></el-input>
    </el-form-item>
    <el-form-item label="胜" prop="win">
      <el-input v-model="dataForm.win" placeholder="胜"></el-input>
    </el-form-item>
    <el-form-item label="负" prop="fail">
      <el-input v-model="dataForm.fail" placeholder="负"></el-input>
    </el-form-item>
    <el-form-item label="场均得分" prop="score">
      <el-input v-model="dataForm.score" placeholder="场均得分"></el-input>
    </el-form-item>
    <el-form-item label="场均失分" prop="loseScore">
      <el-input v-model="dataForm.loseScore" placeholder="场均失分"></el-input>
    </el-form-item>
    <el-form-item label="场均篮板" prop="rebounds">
      <el-input v-model="dataForm.rebounds" placeholder="场均篮板"></el-input>
    </el-form-item>
    <el-form-item label="场均助攻" prop="assists">
      <el-input v-model="dataForm.assists" placeholder="场均助攻"></el-input>
    </el-form-item>
    <el-form-item label="场均抢断" prop="steals">
      <el-input v-model="dataForm.steals" placeholder="场均抢断"></el-input>
    </el-form-item>
    <el-form-item label="场均盖帽" prop="block">
      <el-input v-model="dataForm.block" placeholder="场均盖帽"></el-input>
    </el-form-item>
    <el-form-item label="投篮命中率" prop="shootPercentage">
      <el-input v-model="dataForm.shootPercentage" placeholder="投篮命中率"></el-input>
    </el-form-item>
    <el-form-item label="三分命中率" prop="threePercentage">
      <el-input v-model="dataForm.threePercentage" placeholder="三分命中率"></el-input>
    </el-form-item>
    <el-form-item label="罚球命中率" prop="freePercentage">
      <el-input v-model="dataForm.freePercentage" placeholder="罚球命中率"></el-input>
    </el-form-item>
    <el-form-item label="场均失误" prop="fault">
      <el-input v-model="dataForm.fault" placeholder="场均失误"></el-input>
    </el-form-item>
    <el-form-item label="场均犯规" prop="foul">
      <el-input v-model="dataForm.foul" placeholder="场均犯规"></el-input>
    </el-form-item>
    <el-form-item label="正负值" prop="pna">
      <el-input v-model="dataForm.pna" placeholder="正负值"></el-input>
    </el-form-item>
    <el-form-item label="替补得分" prop="alternate">
      <el-input v-model="dataForm.alternate" placeholder="替补得分"></el-input>
    </el-form-item>
    <el-form-item label="赛季" prop="season">
      <el-input v-model="dataForm.season" placeholder="赛季"></el-input>
    </el-form-item>
    <el-form-item label="常规赛/季后赛/总决赛" prop="match">
      <el-input v-model="dataForm.match" placeholder="常规赛/季后赛/总决赛"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          teamId: '',
          win: '',
          fail: '',
          score: '',
          loseScore: '',
          rebounds: '',
          assists: '',
          steals: '',
          block: '',
          shootPercentage: '',
          threePercentage: '',
          freePercentage: '',
          fault: '',
          foul: '',
          pna: '',
          alternate: '',
          season: '',
          match: ''
        },
        dataRule: {
          teamId: [
            { required: true, message: '球队id不能为空', trigger: 'blur' }
          ],
          win: [
            { required: true, message: '胜不能为空', trigger: 'blur' }
          ],
          fail: [
            { required: true, message: '负不能为空', trigger: 'blur' }
          ],
          score: [
            { required: true, message: '场均得分不能为空', trigger: 'blur' }
          ],
          loseScore: [
            { required: true, message: '场均失分不能为空', trigger: 'blur' }
          ],
          rebounds: [
            { required: true, message: '场均篮板不能为空', trigger: 'blur' }
          ],
          assists: [
            { required: true, message: '场均助攻不能为空', trigger: 'blur' }
          ],
          steals: [
            { required: true, message: '场均抢断不能为空', trigger: 'blur' }
          ],
          block: [
            { required: true, message: '场均盖帽不能为空', trigger: 'blur' }
          ],
          shootPercentage: [
            { required: true, message: '投篮命中率不能为空', trigger: 'blur' }
          ],
          threePercentage: [
            { required: true, message: '三分命中率不能为空', trigger: 'blur' }
          ],
          freePercentage: [
            { required: true, message: '罚球命中率不能为空', trigger: 'blur' }
          ],
          fault: [
            { required: true, message: '场均失误不能为空', trigger: 'blur' }
          ],
          foul: [
            { required: true, message: '场均犯规不能为空', trigger: 'blur' }
          ],
          pna: [
            { required: true, message: '正负值不能为空', trigger: 'blur' }
          ],
          alternate: [
            { required: true, message: '替补得分不能为空', trigger: 'blur' }
          ],
          season: [
            { required: true, message: '赛季不能为空', trigger: 'blur' }
          ],
          match: [
            { required: true, message: '常规赛/季后赛/总决赛不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/team/teambasicdate/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.teamId = data.teamBasicdate.teamId
                this.dataForm.win = data.teamBasicdate.win
                this.dataForm.fail = data.teamBasicdate.fail
                this.dataForm.score = data.teamBasicdate.score
                this.dataForm.loseScore = data.teamBasicdate.loseScore
                this.dataForm.rebounds = data.teamBasicdate.rebounds
                this.dataForm.assists = data.teamBasicdate.assists
                this.dataForm.steals = data.teamBasicdate.steals
                this.dataForm.block = data.teamBasicdate.block
                this.dataForm.shootPercentage = data.teamBasicdate.shootPercentage
                this.dataForm.threePercentage = data.teamBasicdate.threePercentage
                this.dataForm.freePercentage = data.teamBasicdate.freePercentage
                this.dataForm.fault = data.teamBasicdate.fault
                this.dataForm.foul = data.teamBasicdate.foul
                this.dataForm.pna = data.teamBasicdate.pna
                this.dataForm.alternate = data.teamBasicdate.alternate
                this.dataForm.season = data.teamBasicdate.season
                this.dataForm.match = data.teamBasicdate.match
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/team/teambasicdate/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'teamId': this.dataForm.teamId,
                'win': this.dataForm.win,
                'fail': this.dataForm.fail,
                'score': this.dataForm.score,
                'loseScore': this.dataForm.loseScore,
                'rebounds': this.dataForm.rebounds,
                'assists': this.dataForm.assists,
                'steals': this.dataForm.steals,
                'block': this.dataForm.block,
                'shootPercentage': this.dataForm.shootPercentage,
                'threePercentage': this.dataForm.threePercentage,
                'freePercentage': this.dataForm.freePercentage,
                'fault': this.dataForm.fault,
                'foul': this.dataForm.foul,
                'pna': this.dataForm.pna,
                'alternate': this.dataForm.alternate,
                'season': this.dataForm.season,
                'match': this.dataForm.match
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
